package com.mpsc.multi.source.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.mpsc.api.bean.RoleInfoEntity;
import com.mpsc.multi.source.service.RoleInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/roleInfo")
@Api(value = "RoleInfoController", tags = "角色管理")
public class RoleInfoController {

    @Autowired
    private RoleInfoService roleInfoService;

    @PostMapping("/saveRoleInfo")
    @ApiOperation("保存角色信息信息")
    @LcnTransaction(propagation=DTXPropagation.REQUIRED)
    public void saveRoleInfo(@RequestBody RoleInfoEntity role){
        roleInfoService.saveRoleInfo(role);
    }


    @PostMapping("/findSaveRoleInfo")
    @ApiOperation("保存角色信息信息")
    public List<RoleInfoEntity> findSaveRoleInfo(@RequestBody RoleInfoEntity role){
        List<RoleInfoEntity> roleInfoList = roleInfoService.findSaveRoleInfo(role);
        return roleInfoList;
    }

    public void main(String[] args) {
        String businessKey;
        Set<String> businessKeys = new HashSet<>();
//        FileReader reader = new FileReader("D:\\bk.txt");
//        BufferedReader bf = new BufferedReader(reader);
//        while ((businessKey = bf.readLine()) != null) {
//            businessKeys.add(businessKey);
//        }112004
//360100000
//        String businessKeyss = "3404117291381790293,3404116431760366127,3404114717100387242,3404098458547037465,3404113204421263208,3404109355088350853,3404105374005171311,3404109768020995560,3404104990777967595,3404115298404535619,3404111842559427362,3404114815250506953,3404105451900868831,3404105509851106521,3404112567707473938,3404114656314436515,3404114281811423169,3404116065005383100,3404116825652189484,3404116347796691480,3404116360862903586,3404112457975246796,3404116802105366816,3404112218445250261,3404112391197459277,3404114742178250907,3404115080205538543,3404114275617213594,3404106448151745703,3404114648648441936,3404107564639879361,3404116676785854732,3404103280662347551,3404111127307620142,3404104741410504654,3404112032452620199,3404117162306278980,3404119142348741142,3404108480049897861,3404102682697705123,3404119810380048839,3404116821518702888";
//        businessKeys = this.getBusinessKeys(businessKeyss);
//        JSONObject startProcessReq = new TaskHelper()
//                .makeStartProcessParam(businessKeys, "112004", "360100000", "141106");
//        String  ret = JSON.toJSONString(startProcessReq, SerializerFeature.DisableCircularReferenceDetect);
//        System.out.println(ret);
    }

    public Set<String> getBusinessKeys(String businessKeys){
        String[] businessKeyArry = businessKeys.split(",");
        Set<String> businessKeySet = new HashSet<String>(Arrays.asList(businessKeyArry));
        return businessKeySet;
    }

    /**
     * 启动流程入参
     * @param businessKeys
     * @param bizTypeCode
     * @param rgCode
     * @return
     */
    public JSONObject makeStartProcessParam(Collection<String> businessKeys, String bizTypeCode, String rgCode, String variable) {
        // 组装入参
        JSONObject req = new JSONObject();
        req.put("userCode","999111");
        JSONArray paramArray = new JSONArray();
        for(String businessKey: businessKeys) {
            JSONObject paramObj = new JSONObject();
            paramObj.put("bizTypeCode", bizTypeCode);
            paramObj.put("rgCode", rgCode);
            paramObj.put("businessKey", businessKey);
            JSONArray varArray = new JSONArray();
            JSONObject var1 = new JSONObject();
            var1.put("name","menuCode");
            var1.put("type","string");
            var1.put("value", "00111502001" );
            varArray.add(var1);
            paramObj.put("variables", varArray);
            paramArray.add(paramObj);
        }
        req.put("batchProcessInstanceCreateRequest", paramArray);

        return req;
    }

    /**
     * 启动流程入参
     * @param businessKeys
     * @return
     */
    public  JSONObject  makeStartProcessParam(Collection<String> businessKeys) {
        // 组装入参
        JSONObject req = new JSONObject();
        req.put("userCode","999800002");
        JSONArray paramArray = new JSONArray();
        for(String businessKey: businessKeys) {
            // 单据对应的所有变量
            //businessKey,region,bizTypeCode,var
            String[] businessKeyVar = businessKey.split(",");
            JSONObject paramObj = new JSONObject();
            //paramObj.put("bizTypeCode","602");
            //paramObj.put("rgCode", "36");
            paramObj.put("bizTypeCode", businessKeyVar[2]);
            paramObj.put("rgCode", businessKeyVar[1]);
            paramObj.put("businessKey", businessKeyVar[0]);
            JSONArray varArray = new JSONArray();
            JSONObject var = new JSONObject();
            var.put("name","billtype_code");
            var.put("type","string");
            var.put("value", businessKeyVar[3]);
            varArray.add(var);
            //varArray.add(var2);
            //varArray.add(var3);
            paramObj.put("variables", varArray);
            paramArray.add(paramObj);

        }
        req.put("batchProcessInstanceCreateRequest", paramArray);

        return req;
    }
}
